#-*- coding:utf-8 -*-
__author__ = "aesop"

import get_conn
import get_mongo_con
import time

#mysql部分，mysql_material表

#设定素材管理的mysql部分，主要是索引信息 @TODO 编辑时间段
def __new_material_mysql_part(args):
    conn = get_conn.get_conn()
    cur = conn.cursor()

    #u不可缺
    SQL_temp = u'INSERT INTO material_owner(generate_time,material_sub_id,stu_id,edit_time_period)'\
                    'VALUES ("%(time)s","%(topic_id)s","%(stu_id)s","%(edit_time_period)s")'
    SQL = SQL_temp %(args)

    try:
        r = cur.execute(SQL)
        the_insert_id = int(cur.lastrowid)
        conn.commit()
        cur.close()
        conn.close()
        return the_insert_id
    except:
        conn.rollback()
        cur.close()
        conn.close()
        return False




#mongoDb部分，内容数据，包括每个material的内容以及其pv等，以及评论内容
#参数所需key如下obj所示
def __new_material_mongo_part(args):
     material_content = get_mongo_con.get_collection("material_content")
     #字典
     obj = {
         "author":{"stu_id":args["stu_id"], "name":args["name"]},
          "date":args["time"],
         "id":args["id"],
         "topic_id":args['topic_id'],
         "classify":0,
         "tag":[u'愚吉',],
         "title":args['title'],
         "content":args['content'],
         "comment":[]
     }
     material_content.insert(obj)
     return True



def new_material(args):
    if(args.has_key("topic_id") and int(args["topic_id"]) >0) :
        pass
    else:
        args["topic_id"] = int(time.time())
    timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
    args["time"] = timestamp

    insert_id = __new_material_mysql_part(args)
    if(insert_id != False):
        args['id'] = insert_id
        __new_material_mongo_part(args)
        return [insert_id,args['topic_id']]
    else:
        return False



def get_content_material_by_id(material_id):
    '根据id获取material'
    if(material_id):
         material_content = get_mongo_con.get_collection("material_content")
         result = material_content.find_one({'id': int(material_id)})
         return  result
    else:
        return False


def get_latest_material(material_sub_id):
    '获取最近的素材稿子(有效的)'
    conn = get_conn.get_conn()
    cur = conn.cursor()

    SQL = u"SELECT material_id FROM material_owner WHERE material_sub_id = '%s' ORDER BY generate_time  DESC  LIMIT 1 " % material_sub_id
    cur.execute(SQL)
    result = cur.fetchone()
    cur.close()
    conn.close()
    return result[0]


def get_all_material(material_sub_id):
    '获取同意素材下的所有历史'
    conn = get_conn.get_conn()
    cur = conn.cursor()

    SQL = u"SELECT material_id FROM material_owner WHERE material_sub_id = '%s' ORDER BY material_sub_id DESC " % material_sub_id
    cur.execute(SQL)
    result = cur.fetchall()
    cur.close()
    conn.close()
    return result

def get_materials_by_stu_id(stu_id, page = 0):

    '根据学号获取其发布的素材（主题们）'
    conn = get_conn.get_conn()
    cur = conn.cursor()

    SQL = u"SELECT DISTINCT material_sub_id FROM material_owner WHERE stu_id = '%s' ORDER BY material_sub_id DESC  LIMIT %s,5" % (stu_id, page*5)
    cur.execute(SQL)
    result = cur.fetchall()
    cur.close()
    conn.close()
    return [x[0] for x in result ]

if __name__ == "__main__":
    stu_id = 121270026
    print get_materials_by_stu_id(stu_id)

